import __config from '../config/env';

const request = (url, method, data, showLoading) => {
    let _url = __config.basePath + url;

    return new Promise((resolve, reject) => {
        if (showLoading) {
            uni.showLoading({
                title: '加载中...',
                mask: true

            });

        }

        uni.request({
            url: _url,
            method: method,
            data: data,
            header: {
                'app-id': uni.getAccountInfoSync().miniProgram.appId,
                'third-session': getApp().globalData.thirdSession != null ? getApp().globalData
                    .thirdSession : '',
                'OAUTH_BEARER_TOKEN': getApp().globalData.OAUTH_BEARER_TOKEN != null ? getApp()
                    .globalData.OAUTH_BEARER_TOKEN : ''
            },

            success(res) {
                if (res.statusCode == 200 && res.data.code != 60002) {
                    if (res.data.code != 0) {
                        //console.log(res.data);
                        // uni.showModal({
                        // 	title: '提示',
                        // 	content: res.data.msg + '',
                        // 	success: function(res1) {
                        // 		if (res.data.msg == "请先登录") {
                        // 			if (res1.confirm) {
                        // 				uni.switchTab({
                        // 					url: "/pages/user/user-center/index"
                        // 				})
                        // 			} else if (res1.cancel) {
                        // 				uni.switchTab({
                        // 					url: "/pages/home/index"
                        // 				})
                        // 			}
                        // 		}
                        // 	},

                        // 	complete() {
                        // 		if (res.data.code == 60001) {
                        // 			//session过期，则清除过期session，并重新加载当前页
                        // 			uni.setStorageSync('thirdSession', null)
                        // 			getApp().globalData.thirdSession = null;
                        // 			uni.reLaunch({
                        // 				url: getApp().globalData.getCurrentPageUrlWithArgs()
                        // 			});
                        // 		}
                        // 	}
                        // });
                        //reject(res.data.msg);
                        resolve(res.data);
                    }
                    resolve(res.data);
                } else if (res.statusCode == 404) {
                    uni.showModal({
                        title: '提示',
                        content: '接口请求出错，请检查手机网络',
                        success(res) {}
                    });
                    reject();
                } else if (res.statusCode == 500 && res.msg == "获取微信用户信息失败，请重新登录") {
                    uni.showModal({
                        title: '提示',
                        content: '获取微信用户信息失败，请重新登录',
                        success: function(res1) {
                            if (res1.confirm) {
                                uni.switchTab({
                                    url: "/pages/user/user-center/index"
                                })
                            } else if (res1.cancel) {}
                        }
                    });
                } else if (res.data.code == 60002) {} else {
                    console.log("123456789", res);
                    uni.showModal({
                        title: '提示',
                        content: res.errMsg + ':' + res.data.message + ':' + res.data.msg,
                        success(res) {}
                    });
                    reject();
                }
            },

            fail(error) {
                console.log(error);
                uni.showModal({
                    title: '提示',
                    content: '接口请求出错：' + error.errMsg,
                    success(res) {}
                });
                reject(error);
            },
            complete(res) {
                uni.hideLoading();
            }

        });
    });
};
/**
 * 小程序的promise没有finally方法，自己扩展下
 */


Promise.prototype.finally = function(callback) {
    var Promise = this.constructor;
    return this.then(function(value) {
        Promise.resolve(callback()).then(function() {
            return value;
        });
    }, function(reason) {
        Promise.resolve(callback()).then(function() {
            throw reason;
        });
    });
};

module.exports = {
    request,

    // //小区列表，不带搜索
    // app.globalData.api.getPropertyAndPay({
    // 	payDate: '2021-4'
    // }).then(res => {
    // 	that.initInfo = res.resultObj
    // })
    // //详细记录，name为关键字查询，可以搜索业主姓名，电话，楼层单元等
    // app.globalData.api.getPropertyPayById(2, {
    // 	payDate: '',
    // 	name: ''
    // }).then(res => {
    // 	that.initInfo = res.resultObj
    // })

    getMerchantCoupon: (data) => {
        //个人商家优惠券
        return request('/PERSONAL_CENTER/MERCHANT_COUPON', 'post', data, true);
    },
    getPersonnalCoupon: (page, limit, data) => {
        //个人中心优惠券
        return request('/PERSONAL_CENTER/PERSONAL_CENTER_COUPON?page=' + page + '&limit=' + limit, 'post', data,
            true);
    },


    getBusinessBysort: (page, limit, data) => {
        //首页商家推荐列表
        return request('/HOME/BUSINESS_DATA_BYSORT?page=' + page + '&limit=' + limit, 'post', data, false);
    },



    wxPay: (price) => {
        //微信支付,传进来的参数为价格,单位是元
        return request('/PAY/PAY_WECHAT', 'post', price, false);
    },

    getPropertyAndPay: (data) => {
        //所有物业信息及缴费统计
        return request('/PROPERTY/GET_PROPERTY_PAYMENT_FORMATION', 'get', data, true);
    },


    getPropertyPayById: (porpertyId, data) => {
        //某物业下所有缴费信息
        return request('/PROPERTY/GET_ALL_USER_PAYMENT_BY_PROPERTY/' + porpertyId, 'get', data, true);
    },

    checkCoupon: (data) => {
        //核销优惠券，核对信息
        return request('/COUPON/CHECK', 'get', data, true);
    },
    useCoupon: (data) => {
        //使用优惠券
        return request('/COUPON/USE', 'get', data, true);
    },


    addGoods: (data) => {
        //新增商品
        return request('/BACKSTAGE/ADD_PRODUCT', 'post', data, true);
    },
    getGoods: (id) => {
        //获取商品
        return request('/BACKSTAGE/GET_PRODUCT/' + id, 'get', null, true);
    },
    putGoods: (data) => {
        //修改商品
        return request('/BACKSTAGE/UPDATE_PRODUCT', 'post', data, true);
    },
    deleteGoods: (id) => {
        //删除商品
        return request('/BACKSTAGE/DELETE_PRODUCT/' + id, 'post', null, true);
    },

    getGoodsByBussinessIdNoLimit: (merchantId) => {
        //无限制获取商家的所有商品信息
        return request('/BACKSTAGE/GET_PRODUCT_BY_BUNINESS_NOLIMIT/' + merchantId, 'get', null, true);
    },




    addEmp: (empId, empPhone, merchantId) => {
        //添加员工
        return request('/BACKSTAGE/ADD_EMPLOYEE/' + empId + '/' + empPhone + '/' + merchantId, 'get', null,
            false);
    },


    searchPropertys: (page, limit, data) => {
        //物业信息列表搜索
        return request('/PROPERTY/PROPERTY_QUERY?page=' + page + '&limit=' + limit, 'post', data, false);
    },



    getBusiness: (page, limit, data) => {
        //首页商家列表
        return request('/HOME/BUSINESS_DATA?page=' + page + '&limit=' + limit, 'post', data, false);
    },
    getCarousel: () => {
        //首页轮播图和图标搜索
        return request('/HOME/CAROUSEL_DIAGRAMS_ICONS', 'get', null, false);
    },
    getBusinessDetails: (id) => {
        //商家具体信息
        return request('/HOME/MERCHANT_DETAILS?id=' + id, 'post', null, false);
    },
    getCategory: () => {
        //获取所有分类
        return request('/HOME/GET_CATEGORY', 'get', null, false);
    },
    getBusinessDetails: (id) => {
        //商家具体信息
        return request('/HOME/MERCHANT_DETAILS?id=' + id, 'post', null, false);
    },
    getPropertys: (page, limit) => {
        //物业信息列表
        return request('/PROPERTY/PROPERTY_ALL?page=' + page + '&limit=' + limit, 'post', null, false);
    },
    getCouponsByMerchantId: (query) => {
        //商家所有优惠券
        return request('/COUPON/COUPON_LIST', 'get', query, false);
    },
    addUserCoupon: (data) => {
        //领取优惠券
        return request('/COUPON/GET_COUPONS', 'get', data, false);
    },
    getCouponPackage: (data) => {
        //优惠卷包分页列表
        return request('/COUPON_PACKAGE/COUPON_PAGE', 'post', data, false);
    },
    getCouponByPackageId: (id) => {
        //优惠卷包下的所有券
        return request('/COUPON/GET_BY_PACKAGE_ID/' + id, 'get', null, false);
    },
    buyPackage: (packageId, userId) => {
        //优惠卷包下的所有券
        return request('/COUPON_PACKAGE/BUY_PACKAGE/' + packageId + '/' + userId, 'get', null, false);
    },
    getCouponDetail: (id) => {
        //券详情
        return request('/COUPON/COUPON_DETAILS/' + id, 'get', null, false);
    },
    programLogin: (data) => {
        //小程序登录接口
        return request('/PERSONAL_CENTER/PROGRAM_LOGIN', 'post', data, true);
    },
    login: (data) => {
        //小程序登录接口
        return request('/PERSONAL_CENTER/LOGIN', 'post', data, true);
    },
    getUser: () => {
        //小程序登录接口
        return request('/PERSONAL_CENTER/GET_USER', 'post', null, true);
    },
    updateUserInfo: (data) => {
        //更新用户信息
        return request('/PERSONAL_CENTER/UPDATE_USER', 'post', data, true);
    },
    // getPersonnalCoupon: (data) => {
    // 	//个人中心优惠券
    // 	return request('/PERSONAL_CENTER/PERSONAL_CENTER_COUPON', 'post', data, true);
    // },
    savetPropertyPay: (data) => {
        //用户物业缴费
        return request('/PROPERTY/USER_PAYMENT_FORMATION', 'post', data, true);
    },
    getUserPropertyPay: () => {
        //用户物业缴费记录
        return request('/PROPERTY/GET_USER_PAYMENT_FORMATION', 'get', null, true);
    },
    getALLUserPropertyPay: (porpertyId) => {
        //所有用户物业缴费记录
        return request('/PROPERTY/GET_ALL_USER_PAYMENT_FORMATION/' + porpertyId, 'get', null, true);
    },
    getEEPayment: () => {
        //所有用户物业缴费记录
        return request('/PROPERTY/GET_EE_PAYMENT', 'get', null, true);
    },
    addBusiness: (data) => {
        //新增商家
        return request('/BACKSTAGE/ADD_BUSINESS', 'post', data, true);
    },
    getBusinessById: (data) => {
        //获取商家
        return request('/BACKSTAGE/BUSINESS_INFORMATION_DETAILS', 'post', data, true);
    },
    getBusinessByOwnerId: (ownerId) => {
        //获取商家通过所有者ID
        return request('/BACKSTAGE/GET_BUSINESS_BY_OWNER/' + ownerId, 'get', null, true);
    },
    updateBusiness: (data) => {
        //修改商家
        return request('/BACKSTAGE/PUT_BUSINESS', 'post', data, true);
    },
    addUserScore: (data) => {
        //新增积分
        return request('/USER_SCORE/ADD_USER_SCORE', 'post', data, true);
    },
    getUserScore: (userId) => {
        //查询用户积分记录
        return request('/USER_SCORE/GET_USER_SCORE/' + userId, 'get', null, true);
    },
    giveUserScore: (data) => {
        //赠送其他用户积分
        return request('/USER_SCORE/GIVE_USER_SCORE', 'post', data, true);
    },
    getUserById: (id) => {
        //根据用户ID获取用户信息
        return request('/USER_SCORE/GET_USER/' + id, 'get', null, true);
    },
    getGoodsByBussinessId: (merchantId) => {
        //获取商家的所有商品信息
        return request('/BACKSTAGE/GET_PRODUCT_BY_BUNINESS/' + merchantId, 'get', null, true);
    },

    getGoods: (id) => {
        //新增商品
        return request('/BACKSTAGE/GET_PRODUCT/' + id, 'get', null, true);
    },
    getRandBusiness: () => {
        //随机商家列表
        return request('/PERSONAL_CENTER/GET_RAND_BUSINESS', 'get', null, true);
    },


    // login: data => {
    // 	//小程序登录接口
    // 	return request('/weixin/api/ma/wxuser/loginByPhone', 'post', data, false);
    // },
    guest: data => {
        //小程序登录接口
        return request('/weixin/api/ma/wxuser/guest', 'post', data, false);
    },

    wxUserGet: data => {
        //微信用户查询
        return request('/mall/api/ma/wxuser', 'get', null, false);
    },
    wxUserSave: data => {
        //微信用户新增
        return request('/mall/api/ma/wxuser', 'post', data, true);
    },
    noticeGet: data => {
        //轮播图
        return request('/mall/api/ma/notice', 'get', data, true);
    },
    orderCountAll: data => {
        //订单计数
        return request('/mall/api/ma/orderinfo/countAll', 'get', data, false);
    },


    qrCodeUnlimited: data => {
        //获取小程序二维码
        return request('/weixin/api/ma/wxqrcode/unlimited', 'post', data, true);
    },

    getRecruitmentList: data => {
        //获取职位信息
        return request('/gongbaitongapp/api/job/page', 'get', data, true);
    },

    getjobInfo: data => {
        //根据id获取职位信息
        return request('/gongbaitongapp/api/job/detail', 'post', data, true);
    },
    getDictInfo: data => {
        //获取字典信息
        return request('/upms/api/dict/type/' + data, 'get', null, true);
    },
    getRecruitmentCategory: data => {
        //获取职业分类
        return request('/gongbaitongapp/api/jobcategory/list', 'get', null, true);
    },
    jobappLication: data => {
        //职位报名
        return request('/gongbaitongapp/api/jobapplication/saveJob', 'post', data, true);
    },
    jobappLicationModiy: data => {
        //职位变更
        return request('/gongbaitongapp/api/jobapplication/status', 'put', data, true);
    },
    getBaomingInfo: data => {
        //查询报名记录
        return request('/gongbaitongapp/api/recruitmentapply/isBaoMing', 'post', data, true);
    },
    gbtUserInfoGet: () => {
        //查询工百通用户信息
        return request('/gongbaitongapp/api/user', 'get', null, false);
    },
    gbtUsermodif: data => {
        //修改工百通用户信息
        return request('/gongbaitongapp/api/user', 'PUT', data, false);
    },
    gbtWxUserSave: data => {
        //微信用户新增
        return request('/gongbaitongapp/api/wxuser', 'post', data, true);
    },
    getCompanyInfo: data => {
        //查询所有报名记录
        return request('/gongbaitongapp/api/recruitmentapply/all/', 'post', data, true);
    },
    getstation: data => {
        //站点列表
        return request('/gongbaitongapp/api/station/page', 'GET', data, true);
    },
    getstationById: id => {
        //根据ID查询站点
        return request('/gongbaitongapp/api/station/' + id, 'GET', null, true);
    },
    getemployer: data => {
        //雇主信息
        return request('/gongbaitongapp/api/employer/page', 'GET', data, true);
    },
    getemployerById: id => {
        //根据ID查询雇主信息
        return request('/gongbaitongapp/api/employer/' + id, 'GET', null, true);
    },
    jobinTention: data => {
        //求职意向表新增
        return request('/gongbaitongapp/api/jobintention', 'post', data, true);
    },
    getAgent: data => {
        //经纪人获取
        return request('/gongbaitongapp/api/agent/list', 'post', data, true);
    },
    agentApplication: data => {
        //申请成为经纪人
        return request('/gongbaitongapp/api/agentapplication', 'post', data, true);
    },
    agentApplicationModify: data => {
        //申请成为经纪人
        return request('/gongbaitongapp/api/agentapplication', 'put', data, true);
    },
    getAgentApplication: id => {
        //查询用户申请成为经纪人记录
        return request('/gongbaitongapp/api/agentapplication/user/' + id, 'get', null, true);
    },
    getagentById: id => {
        //根据ID查询经纪人
        return request('/gongbaitongapp/api/agent/' + id, 'GET', null, true);
    },
    jobApplication: id => {
        //根据用户id查询报名
        return request('/gongbaitongapp/api/jobapplication/user/' + id, 'GET', null, true);
    },
    modiyApplication: data => {
        //职位申请表修改
        return request('/gongbaitongapp/api/jobapplication', 'put', data, true);
    },
    getSchool: data => {
        //职校列表
        return request('/gongbaitongapp/api/school/page', 'GET', data, true);
    },
    getSchoolByid: data => {
        //查询职校详情
        return request('/gongbaitongapp/api/school/detail', 'post', data, true);
    },
    // schoolappLication: data => {
    // 	//职校报名
    // 	return request('/gongbaitongapp/api/jobapplication/saveSchool', 'post', data, true);
    // },
    getHotJob: data => {
        //热门职位查询
        return request('/gongbaitongapp/api/job/hotJob', 'GET', data, true);
    },
    getTodayJob: data => {
        //今日热招职位查询
        return request('/gongbaitongapp/api/job/todayJob', 'GET', data, true);
    },
    getApplyById: id => {
        //根据经纪人id查询职位报名
        return request('/gongbaitongapp/api/jobapplication/agent/job/' + id, 'GET', null, true);
    },
    getApplyByIdSchool: id => {
        //根据经纪人id查询职校报名
        return request('/gongbaitongapp/api/jobapplication/agent/school/' + id, 'GET', null, true);
    },
    getApplyByIdSkill: id => {
        //根据经纪人id查询技能培训报名
        return request('/gongbaitongapp/api/jobapplication/agent/skill/' + id, 'GET', null, true);
    },

    getAgentformUId: id => {
        //根据用户id查询经纪人
        return request('/gongbaitongapp/api/agent/user/' + id, 'GET', null, true);
    },
    saveJobbyAgent: (data, code) => {
        //帮助他人报名职位
        return request('/gongbaitongapp/api/jobapplication/saveJobByAgent/' + code, 'post', data, true);
    },
    getAgentformSt: id => {
        //根据经纪人id查询下级经纪人
        return request('/gongbaitongapp/api/agent/team/' + id, 'GET', null, true);
    },
    getuserformAg: data => {
        //根据经纪人id查询用户信息（求职意向）
        return request('/gongbaitongapp/api/jobintention/agent', 'GET', data, true);
    },
    saveIntentionByAgent: (data, code) => {
        //经纪人为用户登记
        return request('/gongbaitongapp/api/jobintention/saveIntentionByAgent/' + code, 'post', data, true);
    },
    getAgentincome: id => {
        //根据经纪人id查询详细收益
        return request('/gongbaitongapp/api/agentincome/agent/' + id, 'GET', null, true);
    },
    getAd: data => {
        //广告查询
        return request('/gongbaitongapp/api/ad/list', 'GET', null, true);
    },
    getMsg: id => {
        //根据用户id查询消息
        return request('/gongbaitongapp/api/msgcenter/user/' + id, 'GET', null, true);
    },
    getMsgDetail: id => {
        //根据消息id查询消息详情
        return request('/gongbaitongapp/api/msgcenter/' + id, 'GET', null, true);
    },
    getJoblabel: data => {
        //获取岗位标签
        return request('/gongbaitongapp/api/joblabel/list', 'get', null, true);
    },
    getDicByType: data => {
        //获取岗位标签
        return request('/upms/api/dict/type/' + data, 'get', null, true);
    },
    msgCount: id => {
        //消息数量
        return request('/gongbaitongapp/api/msgcenter/user/count/' + id, 'get', null, false)
    },
    modiyAgent: data => {
        //经纪人信息表修改
        return request('/gongbaitongapp/api/agent', 'put', data, true);
    },
    getNewUserByAgentId: data => {
        //新扫码用户查询
        return request('/gongbaitongapp/api/user/newUser/agent', 'get', data, false)
    },
    getRegistUser: data => {
        //登记未报名
        return request('/gongbaitongapp/api/user/registUser/agent', 'get', data, false)
    },
    getAppliedUser: data => {
        //登记已报名
        return request('/gongbaitongapp/api/user/appliedUser/agent', 'get', data, false)
    },
    getCount: id => {
        //新用户，未报名，已报名人数查询
        return request('/gongbaitongapp/api/user/countNum/agent?agentId=' + id, 'get', null, false)
    },
    getBmCount: id => {
        //报名管理新报名人数
        return request('/gongbaitongapp/api/user/countNewAppliedNum/agent?agentId=' + id, 'get', null, false)
    },
    getZaigang: data => {
        //求职报名管理在岗人数查询
        return request('/gongbaitongapp/api/user/onJobUser/agent/agent', 'get', data, false)
    },
    getProvicen: data => {
        //获取省级城市
        return request('/gongbaitongapp/api/city/getProvice', 'get', data, false)
    },
    getChildrens: parentId => {
        //根据父Id查询子集城市
        return request('/gongbaitongapp/api/city/getChildren/' + parentId, 'get', null, false)
    },
    getSkill: data => {
        //技能培训
        return request('/gongbaitongapp/api/skill/page', 'GET', data, true);
    },
    getStilllByid: data => {
        //查询技能培训详情
        return request('/gongbaitongapp/api/skill/detail', 'post', data, true);
    },
    // 抽奖
    startLottery: id => {
        //开始抽奖
        return request('/gongbaitongapp/api/lottery/startLottery/' + id, 'get', null, true);
    },
    getLotteryTimes: id => {
        //获取抽奖次数
        return request('/gongbaitongapp/api/lottery/times/' + id, 'get', null, true);
    },
    getLotteryRecruit: id => {
        //获取招人记录
        return request('/gongbaitongapp/api/lottery/recruit/' + id, 'get', null, true);
    },
    getLotteryRecord: id => {
        //获取中奖记录
        return request('/gongbaitongapp/api/lottery/record/' + id, 'get', null, true);
    },
    recieveRedPacket: id => {
        //领红包
        return request('/gongbaitongapp/api/lottery/recive/' + id, 'get', null, true);
    },
    getActivity: data => {
        //获取活动列表
        return request('/gongbaitongapp/api/activity/all', 'get', data, true);
    },
    getMyPrize: id => {
        //查看券包
        return request('/gongbaitongapp/api/lotteryrecord/myPrize/' + id, 'get', null, true);
    },
    getQuanbao: data => {
        //报名成功后领券
        return request('/gongbaitongapp/api/lotteryrecord/receive', 'post', data, true);
    },
    getJopList: data => {
        //获取活动列表
        return request('/gongbaitongapp/api/jobcategory/topList', 'get', data, true);
    },
    getPhoneCode: phone => {
        //发送短信验证码
        return request('/gongbaitongapp/api/msgcenter/sendCode/' + phone, 'get', null, true);
    },
    getIsValid: id => {
        //查看活动是否有效
        return request('/gongbaitongapp/api/activity/detail/' + id, 'get', null, true);
    },
    getRelation: id => {
        //根据用户id查询上级多层经纪人关系
        return request('/gongbaitongapp/api/agent/relation/' + id, 'get', null, true);
    },
    getAllapply: data => {
        //查询所有报名用户
        return request('/gongbaitongapp/api/jobapplication/all', 'get', data, true);
    },
    getJobCategoryTree: data => {
        //查询技能培训筛选条件
        return request('/gongbaitongapp/api/jobcategory/tree', 'get', data, true);
    },
    getJinengType: data => {
        //获取技能培训分类，高中低
        return request('/gongbaitongapp/api/course/page', 'get', data, true);
    },
    courserelation: data => {
        //技能培训根据条件选择
        return request('/gongbaitongapp/api/courserelation/page', 'get', data, true);
    },
    getDataNum: data => {
        //查询经纪人和劳动力数量第一级
        return request('/gongbaitongapp/api/city/getDataNumFather', 'get', data, true);
    },
    getChildNum: data => {
        //查询经纪人和劳动力数量子级
        return request('/gongbaitongapp/api/city/getDataNumChildren', 'get', data, true);
    },
};
